package com.ikunikun.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

@Configuration
public class RedisConfiguration {
	@Bean
	public <T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
		RedisTemplate<String, T> redisTemplate = new RedisTemplate<>();
		// 设置连接工厂
		redisTemplate.setConnectionFactory(redisConnectionFactory);


		// 设置键的序列化方式（StringRedisSerializer）
		redisTemplate.setKeySerializer(RedisSerializer.string());
		redisTemplate.setHashKeySerializer(RedisSerializer.string());

		GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
		// 指定redis值的序列化方式（GenericJackson2JsonRedisSerializer）
		// 也可以使用自定义序列化器，例如FastJson2JsonRedisSerializer，需要自定义序列化器，并配置
//		redisTemplate.setValueSerializer(jsonRedisSerializer);
//		redisTemplate.setHashValueSerializer(jsonRedisSerializer);

		return redisTemplate;
	}
}
